#include "StateManager.h"

#include "../GameStates/GameState.h"
#include "../GameStates/MenuState.h"
#include "../GameStates/PlayingState.h"

#include <map>
using namespace std;

//-------------------------------------------------------------------------------------------
StateManager::StateManager()
{
	// create the states.
	m_stateMap[GameStates::STATE_MENU] = (GameState*)new MenuState();
	m_stateMap[GameStates::STATE_PLAYING] = (GameState*)new PlayingState();
}

//-------------------------------------------------------------------------------------------
StateManager::~StateManager()
{
}

//-------------------------------------------------------------------------------------------
void StateManager::Setup()
{
}

//-------------------------------------------------------------------------------------------
GameState* StateManager::GetActiveState()
{
	// Make sure the state your looking for exists, first.
	if (m_stateMap.find(m_activeState) != m_stateMap.end())
	{
		return m_stateMap[m_activeState];
	}

	return 0;
}

//-------------------------------------------------------------------------------------------
void StateManager::SetState(GameStates::Value state)
{
	GameState* pActiveState = GetActiveState();

	// End the old state
	if (pActiveState)
		pActiveState->End();

	// Set the new state
	m_activeState = state;

	pActiveState = GetActiveState();

	// Begin the new state
	if (pActiveState)
		pActiveState->Begin();
}

//-------------------------------------------------------------------------------------------
void StateManager::Tick(float DeltaTime)
{
	GameState* pActiveState = GetActiveState();

	// If no state is active, go to the menu state.
	if (pActiveState)
	{
		pActiveState->Tick(DeltaTime);
	}
	else
	{
		SetState(GameStates::STATE_MENU);
	}
}

//-------------------------------------------------------------------------------------------
void StateManager::OnInput(sf::Event::KeyEvent* pKeyEvent, bool keyDown)
{
	GameState* pActiveState = GetActiveState();

	if (pActiveState)
		pActiveState->OnInput(pKeyEvent, keyDown);
}

//-------------------------------------------------------------------------------------------